from django.shortcuts import render

# Create your views here.

from rest_framework.views import APIView
from rest_framework.response import Response

from utils.phone import is_phone
# , CollegeModel, ProfessionalModel, GradeModel, ClassModel, CourseModel, ChapterModel
from .models import UserModel, TeacherModel, CollegeModel, ProfessionalModel, GradeModel, ClassModel, CourseModel, \
    ChapterModel
from django.core import serializers
import json


# 学生登录
class LoginView(APIView):
    def post(self, request, *args, **kwargs):
        try:
            number = request.POST['number']
            password = request.POST['password']
        except:
            data = {
                'code': 4000,
                'msg': '参数有误'
            }
            return Response(data)
        else:
            # if is_phone(phone):
            user = UserModel.objects.filter(number=number, password=password)
            if len(user) == 0:
                data = {
                    'code': 4000,
                    'msg': '用户不存在'
                }
                return Response(data)
            else:
                user_json = json.loads(serializers.serialize("json", user))[0]
                name = user_json['fields']['name']
                phone = user_json['fields']['phone']
                role = user_json['fields']['role']
                college = user_json['fields']['college']
                professional = user_json['fields']['professional']
                grade = user_json['fields']['grade']
                class_id = user_json['fields']['class_id']
                user_dict = {}
                user_dict['name'] = name
                user_dict['phone'] = phone
                user_dict['role'] = role
                user_dict['college'] = college
                user_dict['professional'] = professional
                user_dict['grade'] = grade
                user_dict['class_id'] = class_id
                user_dict['number'] = number
                data = {
                    'code': 2000,
                    'msg': '登录成功',
                    'data': user_dict
                }
                return Response(data)


# 获取用户信息
class UserDetailsView(APIView):
    def post(self, request, *args, **kwargs):
        try:
            number = request.POST['number']
        except:
            data = {
                'code': 4000,
                'msg': '参数有误'
            }
            return Response(data)
        else:
            user = UserModel.objects.filter(number=number)
            if len(user) == 0:
                data = {
                    'code': 4000,
                    'msg': '用户不存在'
                }
                return Response(data)
            else:
                user_json = json.loads(serializers.serialize("json", user))[0]
                name = user_json['fields']['name']
                phone = user_json['fields']['phone']
                role = user_json['fields']['role']
                college = user_json['fields']['college']
                professional = user_json['fields']['professional']
                grade = user_json['fields']['grade']
                class_id = user_json['fields']['class_id']
                user_dict = {}
                user_dict['name'] = name
                user_dict['phone'] = phone
                if role == 1:
                    user_dict['role'] = "学生"
                elif role == 2:
                    user_dict['role'] = "老师"
                elif role == 3:
                    user_dict['role'] = "管理员"
                user_dict['college'] = college
                user_dict['professional'] = professional
                user_dict['grade'] = grade
                user_dict['class_id'] = class_id
                data = {
                    'code': 2000,
                    'msg': '登录成功',
                    'data': user_dict
                }
                return Response(data)


# 学生修改密码
class ChangePassword(APIView):
    def post(self, request, *args, **kwargs):
        try:
            number = request.POST['number']
            new_password = request.POST['new_password']
        except:
            data = {
                'code': 4000,
                'msg': '参数有误'
            }
            return Response(data)
        else:
            users = UserModel.objects.filter(number=number)
            if len(users) == 0:
                data = {
                    'code': 4000,
                    'msg': '用户不存在'
                }
                return Response(data)
            else:
                UserModel.objects.filter(number=number).update(password=new_password)
                data = {
                    'code': 2000,
                    'msg': '修改成功'
                }
                return Response(data)


# 教师登录
class TeacherLoginView(APIView):
    def post(self, request, *args, **kwargs):
        try:
            number = request.POST['number']
            password = request.POST['password']
        except:
            data = {
                'code': 4000,
                'msg': '参数有误'
            }
            return Response(data)
        else:
            user = TeacherModel.objects.filter(number=number, password=password)
            if len(user) == 0:
                data = {
                    'code': 4000,
                    'msg': '用户不存在'
                }
                return Response(data)
            else:
                user_json = json.loads(serializers.serialize("json", user))[0]
                name = user_json['fields']['number']
                phone = user_json['fields']['name']
                role = user_json['fields']['role']
                user_dict = {}
                user_dict['name'] = name
                user_dict['phone'] = phone
                user_dict['role'] = role
                data = {
                    'code': 2000,
                    'msg': '登录成功',
                    'data': user_dict
                }
                return Response(data)


# 教师修改密码
class TeacherChangePassword(APIView):
    def post(self, request, *args, **kwargs):
        try:
            number = request.POST['number']
            # password = request.POST['password']
            new_password = request.POST['new_password']
        except:
            data = {
                'code': 4000,
                'msg': '参数有误'
            }
            return Response(data)
        else:
            users = TeacherModel.objects.filter(number=number)
            if len(users) == 0:
                data = {
                    'code': 4000,
                    'msg': '用户不存在'
                }
                return Response(data)
            else:
                TeacherModel.objects.filter(number=number).updata(password=new_password)
                data = {
                    'code': 2000,
                    'msg': '修改成功'
                }
                return Response(data)


# 学生查询课程
class StudentQueryCourseView(APIView):
    def post(self, request, *args, **kwargs):
        try:
            number = request.POST['number']
        except:
            data = {
                'code': 4000,
                'msg': '参数有误'
            }
            return Response(data)
        else:
            # 获取班级编号
            class_num = number[0:10]
            c = ClassModel.objects.filter(number=class_num)
            c1 = json.loads(serializers.serialize("json", c))[0]
            course = CourseModel.objects.filter(class_table=c1['pk'])
            course_json = json.loads(serializers.serialize("json", course))
            data_list = []
            for i in course_json:
                data_dict = {}
                data_dict['id'] = i['pk']
                data_dict['course_name'] = i['fields']['name']
                data_dict['course_num'] = i['fields']['number']
                teacher = TeacherModel.objects.filter(id=i['fields']['teacher'])
                teacher_json = json.loads(serializers.serialize("json", teacher))[0]
                data_dict['teacher_name'] = teacher_json['fields']['name']
                data_dict['teacher_num'] = teacher_json['fields']['number']
                data_list.append(data_dict)
            data = {
                'code': 2000,
                'msg': '成功',
                'data': data_list
            }
            return Response(data)


# 查看课程详情
class DetailsCourseView(APIView):
    def post(self, request, *args, **kwargs):
        try:
            course_id = request.POST['course_id']
        except:
            data = {
                'code': 4000,
                'msg': '参数有误'
            }
            return Response(data)
        else:
            c1 = CourseModel.objects.filter(id=course_id)
            c2 = json.loads(serializers.serialize("json", c1))

            if len(c2) == 0:
                data = {
                    'code': 4000,
                    'msg': '没有找到详情'
                }
                return Response(data)
            else:
                data_list = []
                cha1 = ChapterModel.objects.filter(course=c2[0]["pk"])
                if len(cha1)==0:
                    data = {
                        'code': 4000,
                        'msg': '没有找到相应的',
                    }
                    return Response(data)
                else:
                    cha2 = json.loads(serializers.serialize("json", cha1))
                    for i in cha2:
                        data_dict = {}
                        data_dict['cha_name'] = i['fields']['name']
                        data_dict['cha_content'] = i['fields']['content']
                        data_dict['isShowDetail'] = False
                        data_list.append(data_dict)
                    data = {
                        'code': 2000,
                        'msg': '成功',
                        'data': data_list
                    }
                    return Response(data)




# # 获取年级
# class GradeView(APIView):
#     def post(self, request, *args, **kwargs):
#         college_id = request.POST['professional_id']
#         if len(college_id) == 0:
#             data = {
#                 'code': 4000,
#                 'msg': "没有找到"
#             }
#             return Response(data)
#         else:
#             professional = GradeModel.objects.filter(professional=college_id).all()
#             if len(professional) == 0:
#                 data = {
#                     'code': 4000,
#                     'msg': "没有找到"
#                 }
#                 return Response(data)
#             else:
#                 college_json = json.loads(serializers.serialize("json", professional))
#                 data_list = []
#                 for i in college_json:
#                     data_dict = {}
#                     data_dict['college'] = i['fields']['grade']
#                     data_dict['college_number'] = i['fields']['number']
#                     data_dict['id'] = i['pk']
#                     data_list.append(data_dict)
#                 data = {
#                     'code': 2000,
#                     'msg': "成功",
#                     'data': data_list
#                 }
#                 return Response(data)
#
#
# # 获取班级
# class ClassView(APIView):
#     def post(self, request, *args, **kwargs):
#         college_id = request.POST['grade_id']
#         if len(college_id) == 0:
#             data = {
#                 'code': 4000,
#                 'msg': "没有找到"
#             }
#             return Response(data)
#         else:
#             professional = ClassModel.objects.filter(grade=college_id).all()
#             if len(professional) == 0:
#                 data = {
#                     'code': 4000,
#                     'msg': "没有找到"
#                 }
#                 return Response(data)
#             else:
#                 college_json = json.loads(serializers.serialize("json", professional))
#                 data_list = []
#                 for i in college_json:
#                     data_dict = {}
#                     data_dict['college'] = i['fields']['class_id']
#                     data_dict['college_number'] = i['fields']['number']
#                     data_dict['id'] = i['pk']
#                     data_list.append(data_dict)
#                 data = {
#                     'code': 2000,
#                     'msg': "成功",
#                     'data': data_list
#                 }
#                 return Response(data)
#
#
# # class CollegeView(APIView):
# #     def get(self, *args, **kwargs):
# #         try:
# #             college = CollegeModel.objects.all()
# #             if len(college) == 0:
# #                 data = {
# #                     'code': 4000,
# #                     'msg': "没有找到"
# #                 }
# #                 return Response(data)
# #             else:
# #                 college_json = json.loads(serializers.serialize("json", college))
# #                 data_list = []
# #                 for i in college_json:
# #                     data_dict = {}
# #                     data_dict['college'] = i['fields']['college']
# #                     data_dict['college_number'] = i['fields']['number']
# #                     data_dict['professional_list'] = []
# #                     p_obj = ProfessionalModel.objects.filter(college=i['pk'])
# #                     if len(p_obj) == 0:
# #                         continue
# #                     else:
# #                         p_json = json.loads(serializers.serialize("json", p_obj))
# #                         for i in p_json:
# #                             pro_dict = {}
# #                             pro_dict['professional'] = i['fields']['professional']
# #                             pro_dict['professional_number'] = i['fields']['number']
# #                             pro_dict['grade'] = []
# #                             data_dict['professional_list'].append(pro_dict)
# #                             g_obj = GradeModel.objects.filter(professional=i['pk'])
# #                             if len(g_obj) == 0:
# #                                 continue
# #                             else:
# #                                 g_json = json.loads(serializers.serialize("json", g_obj))
# #                                 for i in g_json:
# #                                     g_dict = {}
# #                                     g_dict['grade'] = i['fields']['grade']
# #                                     g_dict['grade_number'] = i['fields']['number']
# #                                     g_dict['class_list'] = []
# #                                     pro_dict['grade'].append(g_dict)
# #                                     cla_obj = ClassModel.objects.filter(grade=i['pk'])
# #                                     if len(cla_obj) == 0:
# #                                         continue
# #                                     else:
# #                                         cls_json = json.loads(serializers.serialize("json", cla_obj))
# #                                         for i in cls_json:
# #                                             cla_dict = {}
# #                                             cla_dict['class_id'] = i['fields']['class_id']
# #                                             cla_dict['class_number'] = i['fields']['number']
# #                                             g_dict['class_list'].append(cla_dict)
# #                                         data_list.append(data_dict)
# #                                         data = {
# #                                             'code': 2000,
# #                                             'msg': "成功",
# #                                             'data': data_list
# #                                         }
# #                                         return Response(data)
# #             raise
# #         except:
# #             data = {
# #                 'code': 4000,
# #                 'msg': "没有找到"
# #             }
# #             return Response(data)
#
# # 修改资料
# class ModificationView(APIView):
#     def post(self, request, *args, **kwargs):
#         phone = request.POST['phone']
#         college = request.POST['college']
#         college_number = request.POST['college_number']
#         professional = request.POST['professional']
#         professional_number = request.POST['professional_number']
#         grade = request.POST['grade']
#         grade_number = request.POST['grade_number']
#         class_id = request.POST['class_id']
#         class_number = request.POST['class_number']
#         if len(college) == 0 or len(professional) == 0 or len(grade) == 0 or len(class_id) == 0 or len(
#                 college_number) == 0 or len(professional_number) == 0 or len(grade_number) == 0 or len(
#             class_number) == 0 or len(phone) == 0:
#             data = {
#                 'code': 4000,
#                 'msg': "参数有误",
#             }
#             return Response(data)
#         else:
#             import datetime
#             year = datetime.datetime.now().year
#             if grade_number == '01':
#                 stu_num = str(year) + college_number + professional_number + grade_number + class_number
#                 u1 = UserModel.objects.filter(phone=phone)
#                 if len(u1) == 0:
#                     data = {
#                         'code': '4000',
#                         'msg': "没有找到此用户",
#                     }
#                     return Response(data)
#                 else:
#                     u2 = json.loads(serializers.serialize("json", u1))[0]
#                     user_name = u2['fields']['name']
#                     user_phone = u2['fields']['phone']
#                     user_role = u2['fields']['role']
#                     UserModel.objects.filter(phone=phone).update(number=stu_num)
#                     StudentsModel.objects.create(number=stu_num, college=college, college_number=college_number,
#                                                  professional=professional, professional_number=professional_number,
#                                                  grade=grade, grade_number=grade_number, class_id=class_id,
#                                                  class_number=class_number)
#                     data = {
#                         'code': '2000',
#                         'msg': "信息修改成功",
#                         'data': {'user_name': user_name, "user_phone": user_phone, "user_role": user_role,
#                                  "stu_num": stu_num}
#                     }
#                     return Response(data)
#             elif grade_number == '02':
#                 stu_num = str(int(year) - 1) + college_number + professional_number + grade_number + class_number
#                 u1 = UserModel.objects.filter(phone=phone)
#                 if len(u1) == 0:
#                     data = {
#                         'code': '4000',
#                         'msg': "没有找到此用户",
#                     }
#                     return Response(data)
#                 else:
#                     u2 = json.loads(serializers.serialize("json", u1))[0]
#                     user_name = u2['fields']['name']
#                     user_phone = u2['fields']['phone']
#                     user_role = u2['fields']['role']
#                     UserModel.objects.filter(phone=phone).update(number=stu_num)
#                     StudentsModel.objects.create(number=stu_num, college=college, college_number=college_number,
#                                                  professional=professional, professional_number=professional_number,
#                                                  grade=grade, grade_number=grade_number, class_id=class_id,
#                                                  class_number=class_number)
#                     data = {
#                         'code': '2000',
#                         'msg': "信息修改成功",
#                         'data': {'user_name': user_name, "user_phone": user_phone, "user_role": user_role,
#                                  "stu_num": stu_num}
#                     }
#                     return Response(data)
#             elif grade_number == '03':
#                 stu_num = str(int(year) - 2) + college_number + professional_number + grade_number + class_number
#                 u1 = UserModel.objects.filter(phone=phone)
#                 if len(u1) == 0:
#                     data = {
#                         'code': '4000',
#                         'msg': "没有找到此用户",
#                     }
#                     return Response(data)
#                 else:
#                     u2 = json.loads(serializers.serialize("json", u1))[0]
#                     user_name = u2['fields']['name']
#                     user_phone = u2['fields']['phone']
#                     user_role = u2['fields']['role']
#                     UserModel.objects.filter(phone=phone).update(number=stu_num)
#                     StudentsModel.objects.create(number=stu_num, college=college, college_number=college_number,
#                                                  professional=professional, professional_number=professional_number,
#                                                  grade=grade, grade_number=grade_number, class_id=class_id,
#                                                  class_number=class_number)
#                     data = {
#                         'code': '2000',
#                         'msg': "信息修改成功",
#                         'data': {'user_name': user_name, "user_phone": user_phone, "user_role": user_role,
#                                  "stu_num": stu_num}
#                     }
#                     return Response(data)
#             elif grade_number == '04':
#                 stu_num = str(int(year) - 3) + college_number + professional_number + grade_number + class_number
#                 u1 = UserModel.objects.filter(phone=phone)
#                 if len(u1) == 0:
#                     data = {
#                         'code': '4000',
#                         'msg': "没有找到此用户",
#                     }
#                     return Response(data)
#                 else:
#                     u2 = json.loads(serializers.serialize("json", u1))[0]
#                     user_name = u2['fields']['name']
#                     user_phone = u2['fields']['phone']
#                     user_role = u2['fields']['role']
#                     UserModel.objects.filter(phone=phone).update(number=stu_num)
#                     StudentsModel.objects.create(number=stu_num, college=college, college_number=college_number,
#                                                  professional=professional, professional_number=professional_number,
#                                                  grade=grade, grade_number=grade_number, class_id=class_id,
#                                                  class_number=class_number)
#                     data = {
#                         'code': '2000',
#                         'msg': "信息修改成功",
#                         'data': {'user_name': user_name, "user_phone": user_phone, "user_role": user_role,
#                                  "stu_num": stu_num}
#                     }
#                     return Response(data)
#             else:
#                 data = {
#                     'code': 4000,
#                     'msg': "参数有误",
#                 }
#                 return Response(data)
